# Contributor: Sheila Aman <sheila@vulpine.house>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=protobuf
_gemname=google-protobuf
pkgver=24.4
_rubyver=3.$pkgver
pkgrel=1
pkgdesc="Library for extensible, efficient structure packing"
url="https://github.com/protocolbuffers/protobuf"
arch="all"
license="BSD-3-Clause"
depends="
	protoc=$pkgver-r$pkgrel
	libprotoc=$pkgver-r$pkgrel
	libprotobuf=$pkgver-r$pkgrel
	libprotobuf-lite=$pkgver-r$pkgrel
	"
depends_dev="
	protoc=$pkgver-r$pkgrel
	zlib-dev
	"
makedepends="
	$depends_dev
	abseil-cpp-dev
	cmake
	ruby
	ruby-dev
	ruby-rake
	samurai
	"
checkdepends="gtest-dev"
subpackages="
	ruby-$_gemname:_ruby
	$pkgname-dev
	$pkgname-vim::noarch
	protoc
	libprotoc
	libprotobuf
	libprotobuf-lite
	"
	# exclude.patch
source="$pkgname-$pkgver.tar.gz::https://github.com/protocolbuffers/protobuf/archive/refs/tags/v$pkgver.tar.gz
	exclude.patch
	protoc-cmake.patch
	ruby-fix-cflags.patch
	skip-failing-tests.patch
	soversion.patch
	trim-rakefile.patch
	"

# fails some
case "$CARCH" in
armhf) options="$options !check" ;;
esac

build() {
	export CFLAGS="$CFLAGS -DNDEBUG -O2 -flto=auto"
	export CXXFLAGS="$CXXFLAGS -DNDEBUG -O2 -flto=auto -Wno-deprecated-declarations"

	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DBUILD_SHARED_LIBS=ON \
		-Dprotobuf_BUILD_TESTS="$(want_check && echo ON || echo OFF)" \
		-Dprotobuf_BUILD_LIBPROTOC=ON \
		-Dprotobuf_ABSL_PROVIDER="package" \
		-Dprotobuf_ALLOW_CCACHE=ON \
		-Dprotobuf_USE_EXTERNAL_GTEST=ON
	cmake --build build

	cd "$builddir"/ruby

	export PATH="$PATH:$builddir/build"

	# Generate proto files for built-in protocols.
	rake genproto

	gem build $_gemname.gemspec
	gem install --local \
		--install-dir dist \
		--ignore-dependencies \
		--no-document \
		--verbose \
		$_gemname
}

# TODO: Run tests for ruby gem.
check() {
	ctest --test-dir build --output-on-failure
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	sed -i \
		-e "/Requires:/s|;| |g" \
		-e "/Requires:/s|::|_|g" \
		"$pkgdir"/usr/lib/pkgconfig/protobuf.pc

	install -Dm644 editors/proto.vim \
		"$pkgdir"/usr/share/vim/vimfiles/syntax/proto.vim

	local gemdir="$pkgdir/$(ruby -e 'puts Gem.default_dir')"
	cd ruby/dist

	mkdir -p "$gemdir"
	cp -r extensions gems specifications "$gemdir"/

	# Remove duplicated .so libs (should be only in extensions directory).
	find "$gemdir"/gems/ -name "*.so" -delete

	# Remove unnecessary files.
	cd "$gemdir"/gems/$_gemname-$_rubyver
	rm -r ext/
}

_ruby() {
	depends=""
	pkgdesc="Ruby bindings to Google's data interchange format"

	amove usr/lib/ruby/gems
}

vim() {
	pkgdesc="Vim syntax for $pkgname"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel vim"

	amove usr/share/vim/vimfiles/syntax/proto.vim
}

libprotoc() {
	depends=""
	pkgdesc="Runtime library for Protocol Buffer compiler"

	amove usr/lib/libprotoc.so.*
}

protoc() {
	depends=""
	pkgdesc="Protocol buffer compiler binary and library"

	amove usr/bin/protoc*
}

libprotobuf() {
	depends=""
	pkgdesc="Runtime library for C++ users of protocol buffers"

	amove usr/lib/libprotobuf.so.*
}

lite() {
	depends=""
	pkgdesc="Runtime library for C++ users with 'lite runtime' setting of protocol buffers"

	amove usr/lib/libprotobuf-lite.so.*
}

sha512sums="
52b6ab5587d03cbd1f35cf3cdc388e1710fa50f3031559ac53cf754965407ded7602cdead56080444ab695588112cc3391a1d7fdd5e565d90d0af7ad08706315  protobuf-24.4.tar.gz
0d46d105a0670ab5cfcb04ed534927a71613234a88efede0b20d426e1caaa708e6ff275a09c5dab7786fe26e69e0882f022b893302e4869006b4ac55fc6172e5  exclude.patch
16b8ee0e50dbb2144ddb83654a63e410b4218e667b6b8b77c7b6f9797508f63c3e8be0f9711841bb6ebbed0883cda2394b219a62b468240f65f4dac418b5a56e  protoc-cmake.patch
0cb202a6151ae3746c09e85f2e4ce9abb0d810d4a3c84cb647fde407cf8d13894d3c9b340e06d7a3a85669da1899db265d26d8309d59249f841a40a66e045c83  ruby-fix-cflags.patch
46b6644ccf6e142ef26cddd818466caab2696b685f3865061fe0e45b65cefc5598add4a8479842ca1aae948d37b3c2c6944913c62a00422fa91b694277986c73  skip-failing-tests.patch
850dc1d6fd3ea01f1689fd700ac35e7da0882092ab34c616702e01bdf4b5327b8ab27381ecf291431a94f54c89d31707a4a171929937e880543974cb64f9e685  soversion.patch
13514a456a50d7243baa23fe43586fc4544cda87b9e2a5afe9d2fac8afca8e8853f9b42232c3fff4c5cf2df443af32f944f451daa1d604d80fd03ee4e7abed3e  trim-rakefile.patch
"
